var curr_id = 0;
var load = 1;
var buttons;
var t_time;
var rank;
$("document").ready(function() {
	hello = setInterval(load_tweets, 1500);
});
function reply_vote(vote,iid){
        $.post("post.php", { vote : vote, iid : iid});
}
function submit_tweet(iid){
       	var txt_area = "textarea#text" + iid; 
	var text = $(txt_area).val();	
	$.post("posttweet.php",{ text_content : text ,iid :iid });
	$.post("post.php", { vote: 1, iid : iid});	
	hello = setInterval(load_tweets, 1500);
        document.getElementById(iid).style.display = 'none';
}
//function show_textbox(){
//	$("#resultmessage").html("<div style='display:block; background-color: Green; color: White; font-size: 15px; font-weight: bold; padding: 0px 0px 0pxh0px;'>Saved!")
//	$("#resultmessage").show('slow');
//}
function load_tweets() {
	var result = '';	
	$.get('getinfo.php', function(data){
		var last_id = parseInt($(data).find("last_id").text())
		if ( last_id < curr_id + 20 ){	
			load = 0;
		} else {
			load = 1;
		}
		if ( load == 1 ){
			$(".resultmessage").remove();	
			$(".tweet_result").remove();		
			$(".buttons").remove();	
			$("tweet",data).each(function(){
				curr_id = parseInt($(this).find("id").text())	
				buttons = "<div class = 'buttons'><div id = 'like'><a href='javascript:reply_vote(1," + curr_id +");'>Like</a></div><div id = 'unlike'><a href='javascript:reply_vote(-1,"+ curr_id +");'>Unlike</a></div><div id = 'reply'><a href='javascript:show_textbox(" +curr_id + ");'>Reply</a></div>";	
				var title = $(this).find("title").text();
				rank = "<div id = 'rank'>" + "Rank: " + $(this).find("rank").text();
				t_time = buttons + " " + "<div id = 'time_stamp'>" + $(this).find("time").text() + "</div>" + rank + "</div></div></div>";
				if ( title !='' ){
					var txt_box = "</p><div class='resultmessage' id =" + curr_id + " style:display='none'><form method='post' action=''><textarea class = 'reply' id =" + "text" + curr_id +" name = 'comments' cols='40' rows ='5'></textarea><a href='javascript:submit_tweet(" + curr_id +");'>Submit</a><div id = 'exit'><a href='javascript:show_textbox("+ curr_id +");'>X</a></div></form></div>";	
					result = result + "</p>" + "<p class='tweet_result' id='tweet'>" + linkify(title) + t_time +"</p>" + txt_box;
				}  //$("#twitter_results").append(result);	
			});
			$("#twitter_results").append(result);	
			load = 0;
		}
		//$("#twitter_results").prepend(result);	
	});
}
function show_textbox(id) {
	var state = document.getElementById(id).style.display;
		if (state == 'block') {
			hello = setInterval(load_tweets, 1500);			
			document.getElementById(id).style.display = 'none';
		} else {
			clearInterval( hello );	
			document.getElementById(id).style.display = 'block';
		}
}
function linkify(text) {
    // modified from TwitterGitter by David Walsh (davidwalsh.name)
    // courtesy of Jeremy Parrish (rrish.org)
    return text.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>')
               .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
               .replace(/(^|\W)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
}
